const db = wx.cloud.database();
let that = this;
const app = getApp();

Page({
  data: {
<<<<<<< HEAD
    user_nickname: '',
    user_url: '',
    openid:'',
    activityName: '六月份活动',
=======
    items: [{
        value: 'USA',
        name: '魔法'
      },
      {
        value: 'CHN',
        name: '拼图',
        checked: 'true'
      },
      {
        value: 'BRA',
        name: '乐高'
      },
      {
        value: 'JPN',
        name: '其他'
      }
    ],
    user_openid:'',
    user_nickname: '',
    user_url: '',
    activityName: '初始化',
>>>>>>> 5582414bcd49b661512ceed6033fc139043a438f
    description: '',
    donation_name:'',
    donation_image: '',
    telephoneNumber: '',
    type: '',
    array: ['魔方', '拼图', '乐高', '其他'],
    number: '',
    number: 1 //图片数
  },

  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      items
    })
  },
  // 类型选择
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      items
    })
  },

  onLoad:function(options) {
    
    let userinfo = wx.getStorageSync('app.globalData.userInfo');
<<<<<<< HEAD
    let openid=wx.getStorageSync('app.globalData.userOpenId');
    console.log(userinfo)
=======
    let openid=wx.getStorageSync('app.globalData.userOpenId')
>>>>>>> 5582414bcd49b661512ceed6033fc139043a438f
    this.setData({
      user_openid:openid,
      user_nickName: userinfo.nickName,
      user_url: userinfo.avatarUrl,
      openid:openid
    })
<<<<<<< HEAD
    db.collection('activity').doc('086b8412644b2aa600000e135844f88f').get({
      success: function (res) {
        console.log(res)
=======
    db.collection("activity").doc('086b8412644b2aa600000e135844f88f').get({
      success: res=>{
        console.log(res.data)
        console.log(res.data.name)
>>>>>>> 5582414bcd49b661512ceed6033fc139043a438f
        this.setData({
          activityName: res.data.name
        })
        console.log(activityName)
      }
    })
  },

  onReady() {},

  onShow() {},

  getName: function (e) {
    this.setData({
      donation_name: e.detail.value
    })
  },

  gettele: function (e) {
    this.setData({
      telephoneNumber: e.detail.value
    })
  },

  getInput: function (e) {
    this.setData({
      description: e.detail.value
    })
  },

  uploadImage(fileURL) {
    var that = this
    wx.cloud.uploadFile({
<<<<<<< HEAD
      cloudPath: "userDonation/"+new Date().getTime() + '.png',
=======
      cloudPath: "userDonation/" + app.globalData.userOpenId + new Date().getTime() + '.png',
>>>>>>> 5582414bcd49b661512ceed6033fc139043a438f
      filePath: fileURL,
      success: res => {
        that.addImagePath(res.fileID)
      },
      fail: console.error
    })
  },

  btnsub: function (res) {
    var that = this
    console.log(res)
    var actName = that.data.activityName
    var description = res.detail.value.description
    var donation_name = that.data.donation_name
    var donation_image = that.data.donation_image
    var tele = res.detail.value.telephoneNumber
    var type = that.data.type
    var usrn = that.data.user_nickName
    var usri = that.data.user_url
<<<<<<< HEAD
    var openid=that.data.openid
    var tempFilePaths = that.data.Filepath
    console.log(type)
=======
    var usrid=that.data.userOpenId
    var tempFilePaths = that.data.Filepath
>>>>>>> 5582414bcd49b661512ceed6033fc139043a438f
    if (actName != "" &&  type != "" ) {
      wx.showLoading({
        title: '发布中...',
        mask: true,
        duration: 1000
      }).then(res => {
        db.collection("donation").add({
          data: {
            userOpenId:usrid,
            user_nickname: usrn,
            user_url: usri,
            openid:openid,
            activityName: actName,
            description: description,
            donation_name:donation_name,
            donation_image: donation_image,
            telephoneNumber: tele,
            type: type,
          }
        }).then(res => {
          wx.hideLoading()
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              console.log(res)
            }
          })
          that.setData({
            usrid:'',
            user_nickname:'',
            user_url: '',
            activityName: '',
            description: '',
            donation_name:'',
            donation_image: '',
            telephoneNumber: '',
            type: '',
            number: 1
          })
          wx.switchTab({
            url: '../home/home'
          })
        }, )
      })
      fail:console.error();
    } else {
      wx.showToast({
        icon: 'none',
        title: '请检查输入的数据是否有误！',
        duration: 1000,
        mask: true,
      })
    }
  },

  cancel: function () {
    wx.navigateTo({
      url: '../../pages/home'
    })
  },

  clickimage: function (e) {
    var index = e.target.dataset.index
    wx.previewImage({
      urls: [this.data.donation_image[index]],
    })
  },

  addImage: function (e) {
    var that = this
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log("成功", res);
        that.uploadImage(res.tempFiles[0].tempFilePath);
        that.setData({
          donation_image: res.tempFiles[0].tempFilePath,
          number: res.tempFiles.length + 1
        })
      }
    })
  },

  // deleteImage: function (e) {
  //   var that = this
  //   var index = e.target.dataset.index
  //   var tempFilePaths = that.data.donation_image
  //   wx.showModal({
  //     title: '提示',
  //     content: '确定要删除此图片吗？',
  //     success: function (res) {
  //       if (res.confirm) {
  //         console.log('点击确定了');
  //         that.data.donation_image.splice(index, 1);
  //       } else if (res.cancel) {
  //         console.log('点击取消了');
  //         return false;
  //       }
  //       that.setData({
  //         donation_image: tempFilePaths,
  //         number: that.data.number - 1
  //       });
  //     }
  //   })
  // },

  pickType: function (e) {
    console.log(e)
    this.setData({
      type: this.data.array[e.detail.value]
    })
    console.log(this.data.type)
  },

})